package utils.command;

import java.io.*;

public class CommandLinux implements Command{
    private static final CommandLinux commandLinux;
    private Process process;

    static {
        commandLinux = new CommandLinux();
    }

    CommandLinux(){}

    CommandLinux(String []args) throws IOException {
        process = Runtime.getRuntime().exec(args);
    }//执行单次命令，执行后即停止向终端中输入新命令，不停止输出

    public void getContinuityProcess(File file) throws IOException {
        process=Runtime.getRuntime().exec("/bin/bash",null,file);
    }

    public void getOneTimeProcess(String[] args) throws IOException {
        process=Runtime.getRuntime().exec(args);
    }//执行单次命令，执行后即停止向终端中输入新命令，不停止输出

    public void getOneTimeProcess(String cmd) throws IOException {
        String[] args=new String[]{"/bin/bash","-c",cmd};
        process=Runtime.getRuntime().exec(args);
    }//执行单次命令，执行后即停止向终端中输入新命令，不停止输出

    public OutputStream getOutputStream(){
        return process.getOutputStream();
    }//返回输出流

    public PrintWriter getPrintWriter(){
        return new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    }//返回输出流

    public OutputStream getOutputStream(File file) throws IOException {
        process = Runtime.getRuntime().exec("/bin/bash", null, file);
        return process.getOutputStream();
    }//执行新命令，执行后返回输出流

    public PrintWriter getPrintWriter(File file) throws IOException {
        process = Runtime.getRuntime().exec("/bin/bash", null, file);
        return new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    }//执行新命令，执行后返回输出流

    public InputStream getCmdInputStream(String[] args) throws IOException {
        process = Runtime.getRuntime().exec(args);
        return process.getInputStream();
    }

    public InputStream getCmdInputStream() {
        return process.getInputStream();
    }

    public BufferedReader getErrorBufferedReader(String[] args) throws IOException {
        process = Runtime.getRuntime().exec(args);
        return new BufferedReader(new InputStreamReader(process.getErrorStream()));
    }

    public BufferedReader getErrorBufferedReader(){
        return new BufferedReader(new InputStreamReader(process.getErrorStream()));
    }

    public BufferedReader getInputBufferedReader(String[] args) throws IOException {
        process = Runtime.getRuntime().exec(args);
        return new BufferedReader(new InputStreamReader(process.getInputStream()));
    }

    public BufferedReader getInputBufferedReader(){
        return new BufferedReader(new InputStreamReader(process.getInputStream()));
    }

    @Override
    public String oSCommand(String[] args) throws IOException {
        String output = "";
        Process process = Runtime.getRuntime().exec(args);
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line ;
        while ((line = errorReader.readLine()) != null) {
            output+=line+System.getProperty("line.separator");
        }
        errorReader.close();
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while ((line = infoReader.readLine()) != null) {
            output+=line+System.getProperty("line.separator");
        }
        infoReader.close();
        return output;
    }//执行单次命令，执行后进程结束，停止输入，输出。

    public static String oSCommandStatic(String[] args) throws IOException{
        return commandLinux.oSCommand(args);
    }

    private static void print(String[] args) {
        for (String arg : args) {
            System.out.println(arg);
            System.out.print(" ");
        }
    }

    private static void print(String arg) {
        System.out.println(arg);
    }
}
